<?php
abstract class htmlwidget{
	public $tag;
	public $html;
	public $attributes = array ();
	public $validAttributes = array(
		'id', 
		'style', 
		'class', 
		'dir', 
		'lang',
		'xml:lang', 
		'title',
		'onfocus', 
		'onblur', 
		'onclick', 
		'ondblclick', 
		'onmousedown', 
		'onmouseup', 
		'onmouseover', 
		'onmousemove', 
		'onmouseout', 
		'onkeypress', 
		'onkeydown', 
		'onkeyup'	
	);
	public $validAttributeValues = array();
	
	public function getid(){
		if ( isset($this->attributes['id'])){
			return $this->attributes['id'];
		} else {
			return '';		
		}
	}
	
	public function setid($id){
		$this->attributes['id'] = $id;
	}

	public function getname(){
		if ( isset($this->attributes['name'])){
			return $this->attributes['name'];
		} else {
			return '';
		}
	}
		
	public function setname($name){
		$this->attributes['name'] = $name;
	}	
	
	public function getclass(){
		if ( isset($this->attributes['class'])){
			return $this->attributes['class'];
		}
	}
	
	public function setclass($value){
		$this->attributes['class'] = $value;
	}
	
	public function addclass($value){
		if ( empty($this->attributes['class'])){
			$this->setclass($value);
		} else {
			$this->attributes['class'] = trim($this->attributes['class']) . ' ' . $value;
		}
	}
	
	public function getstyle(){
		if ( isset($this->attributes['style'])){
			return $this->attributes['style'];
		} else {
			trigger_error("Invalid Attribute: [style]  for element of type " . get_class($this), E_USER_ERROR);
		}
	}
	
	public function setstyle($value){
		$this->attributes['style'] = $value;
	}	
	
	public function __toString(){
		return $this->html;
	}

	public function getHtml(){
		return $this->__toString();
	}

	public function draw(){
		print $this->__toString();
	}

	
	public function getAttribute($key){
		$key = strtolower($key);
		if ( in_array($key, $this->validAttributes)){
			if ( isset($this->attributes[$key])){
				return $this->attributes[$key];				
			} else {
				return '';
			}			
		} else {
			trigger_error("Invalid Attribute:" . $key . ' for element of type ' . get_class($this), E_USER_ERROR);
		}
	}
	
	public function setAttribute($key, $value){
		$key = strtolower($key);
		if ( in_array($key, $this->validAttributes)){
			if ( isset($this->validAttributeValues[$key])){
				if ( in_array($value, $this->validAttributeValues[$key])){
					$this->attributes[$key] = $value;				
				} else {
					trigger_error("Invalid Attribute:" . $key . ' for element of type ' . get_class($this), E_USER_ERROR);
				}
			} else {
				$this->attributes[$key] = $value;
			}
		} else {
			trigger_error("Invalid Attribute:" . $key . ' for element of type ' . get_class($this), E_USER_ERROR);
		}
	}
	
	public function setAttrib($key, $value){
		$this->setAttribute($key, $value);
	}

	public function clear(){
		$this->html = '';
		$this->attributes = array();
	}
	
	
	public function randomstring($len=8) {
	    $chars = preg_split("//", 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', null, PREG_SPLIT_NO_EMPTY);
	    $string = '';
	    for ($i = 0; $i < $len; $i++) {
	        $string .= $chars[rand(0, count($chars) -1)];
	    }
	    return $string;
	}		
}
?>